package ru.elifantiev.cityrouter.util;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.ByteArrayOutputStream;


public class DrawableSerializer {

    /**
     * Encodes a Drawable to String in Data:URU format
     * @param res Resource repository
     * @param id Resource id
     * @return Encoding result
     */
    public static String serializeToDataURL(Resources res, int id) {
        ByteArrayOutputStream str = new ByteArrayOutputStream();
        Bitmap icon = BitmapFactory.decodeResource(res, id);
        if(icon == null)
            throw new IllegalArgumentException("No such drawable");

        icon.compress(Bitmap.CompressFormat.PNG, 100, str);
        return "data:image/png;base64," + new String(Base64Coder.encode(str.toByteArray()));
    }

}
